package javacode.secondary_class;

import java.awt.*;
import javax.swing.*;

/**
 *
 *
 */
public class ColorRenderer extends JPanel implements ListCellRenderer {

    int rank = 0;

    public ColorRenderer() {
        setPreferredSize(new Dimension(32, 20));
    }

    @Override
    public void paint(Graphics g) {
        Color myCol;
        int w = getWidth(); // <----------------
        int h = getHeight();

        switch (rank) {
            case 0:
                myCol = Color.WHITE;
                break;
            case 1:
                myCol = Color.BLACK;
                break;
            case 2:
                myCol = Color.BLUE;
                break;
            case 3:
                myCol = Color.GREEN;
                break;
            case 4:
                myCol = Color.RED;
                break;
            case 5:
                myCol = Color.ORANGE;
                break;
            case 6:
                myCol = Color.YELLOW;
                break;
            case 7:
                myCol = Color.PINK;
                break;
            default:
                myCol = Color.RED;
        }

        g.setColor(myCol);
        g.fillRect(0, 0, w, h);

        paintBorder(g);
    }

    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        rank = ((Number) value).intValue();
        if (index < 0) {
            setBorder(null);
        } else {
            setBorder(BorderFactory.createLineBorder(isSelected ? Color.WHITE
                    : Color.WHITE, 2));
        }
        return this;
    }
}
